package com.zslin.business.tools.excel;

import com.zslin.core.tools.ConfigTools;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

@Component
public class ExcelExportUtil {

    @Autowired
    private OrdersExportTools ordersExportTools;

    @Autowired
    private ConfigTools configTools;

    public void export2Response(OutputStream os, String day) {
        List<OrdersDto> dtoList = ordersExportTools.buildData(day);
        try {
//            InputStream is = ClassLoader.getSystemResourceAsStream("publicFile/orders-template.xlsx");
//            File tempFile = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "publicFile/orders-template.xlsx");
            File tempFile = new File(configTools.getFilePath("template/")+"orders-template.xlsx");
            XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(tempFile));
            XSSFSheet sheet = wb.getSheetAt(0);

            int index = 1;
            for(OrdersDto dto : dtoList) {
                XSSFRow row = sheet.createRow(index++);
//                System.out.println(row);
//                System.out.println(dto);
                row.createCell(0).setCellValue(dto.getOrderNo());
                row.createCell(1).setCellValue(dto.getCreateTime());
                row.createCell(2).setCellValue(dto.getPayTime());
                row.createCell(3).setCellValue(dto.getAddress());
                row.createCell(4).setCellValue(dto.getAgentName()+"-"+dto.getAgentPhone());
                row.createCell(5).setCellValue(dto.getDiscountMoney());
                row.createCell(6).setCellValue(dto.getPayMoney());
                row.createCell(7).setCellValue(dto.getTotalMoney());
                row.createCell(8).setCellValue(dto.getProTitles());
                row.createCell(9).setCellValue(dto.getSpecs());
                row.createCell(10).setCellValue(dto.getStatus());
            }
            wb.setSheetName(0, day);

//            FileOutputStream fos = new FileOutputStream("D:/temp/orders-export.xlsx");
            wb.write(os);
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*public static void export(String day, List<OrdersDto> dtoList) {
//        SXSSFWorkbook wb = new SXSSFWorkbook();
//        String path = ClassLoader.getSystemResource("publicFile/orders-template.xlsx").getPath();
        try {
            InputStream is = ClassLoader.getSystemResourceAsStream("publicFile/orders-template.xlsx");
            XSSFWorkbook wb = new XSSFWorkbook(is);
            XSSFSheet sheet = wb.getSheetAt(0);

            int index = 1;
            for(OrdersDto dto : dtoList) {
                XSSFRow row = sheet.createRow(index++);
//                System.out.println(row);
//                System.out.println(dto);
                row.createCell(0).setCellValue(dto.getOrderNo());
                row.createCell(1).setCellValue(dto.getCreateTime());
                row.createCell(2).setCellValue(dto.getPayTime());
                row.createCell(3).setCellValue(dto.getAddress());
                row.createCell(4).setCellValue(dto.getAgentName()+"-"+dto.getAgentPhone());
                row.createCell(5).setCellValue(dto.getDiscountMoney());
                row.createCell(6).setCellValue(dto.getPayMoney());
                row.createCell(7).setCellValue(dto.getTotalMoney());
                row.createCell(8).setCellValue(dto.getProTitles());
                row.createCell(9).setCellValue(dto.getSpecs());
                row.createCell(10).setCellValue(dto.getStatus());
            }
            wb.setSheetName(0, day);

            FileOutputStream fos = new FileOutputStream("D:/temp/orders-export.xlsx");
            wb.write(fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/
}
